using System;
using System.Globalization;
using System.Web.Mvc;
using RMH.DataAccess;
using RMH.Entities;

namespace RMH.Web.Models.Binders
{
    public class SupportPersonBinder : IModelBinder
    {
        private ISupportPersonRepository supportPersonRepository;

        public SupportPersonBinder(ISupportPersonRepository supportPersonRepository)
        {
            this.supportPersonRepository = supportPersonRepository;
        }

        public SupportPersonBinder()
        {
            supportPersonRepository = Container.Resolve<SupportPersonRepository>();
        }

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var form = controllerContext.HttpContext.Request.Form;

            SupportPerson supportPerson = new SupportPerson();

            if (!String.IsNullOrEmpty(form["Id"]))
                supportPerson = supportPersonRepository.GetByID(Int32.Parse(form["Id"]));
            PersonBinder personBinder = new PersonBinder();
            personBinder.BindModel(controllerContext, bindingContext, supportPerson);
            return supportPerson;
        }
    }
}